#include <iostream>
#include <thread>
#include <vector>
#include <barrier>
#include <chrono>
#include <stop_token>

std::barrier barrier(3); // 定义一个barrier，等待3个线程

void task(std::stop_token stop_token, int thread_id) {
    std::cout << "Thread "<< thread_id << " is ready\n";
    barrier.arrive_and_wait(); // 线程到达barrier点，等待其他线程
    std::cout << "Thread "<< thread_id << " proceeds\n";
}

int main() {
    std::vector<std::jthread> threads;

    for (int i = 0; i < 3; ++i) {
        threads.emplace_back(task, i);
        std::this_thread::sleep_for(std::chrono::seconds(1)); // 让线程有机会启动
    }

    return 0;
}